#include "XLpaTab.h"
#include "XWav.h"
#include <QTextStream>
#include <QFileDialog>
#include <QFont>


void XLpaTab::openButtonClicked() {
	out << "Open Button Clicked!" << endl;
	QString fname = QFileDialog::getOpenFileName(this, tr("Open File"), "",
							 tr("LPA files(*.lpa)"));
	QFile f(fname);
	if (f.exists()) {
		fileName = fname;
		fileLabel->setText(fileName);
		outputEdit->setText(deleteExt(fileName) + "-lpa.wav");
		lpa->update(fileName);
	} else {
		out << "File not found!" << endl;
	}
}


void XLpaTab::convertButtonClicked() {
	out << "LPA's Convert Button Clicked!" << endl;
	lpa->convert(outputEdit->text());
}


void XLpaTab::setupUI() {
	QHBoxLayout *tmpLayout;

	// Open & File
	openButton = new QPushButton("Open");
	openButton->setMinimumWidth(60);
	connect(openButton, SIGNAL(clicked()), this, SLOT(openButtonClicked()));
	fileLabel = new QLabel(fileName);
	fileLabel->setFont(QFont("Courier New", 10));
	QHBoxLayout *fileLayout = new QHBoxLayout();
	fileLayout->addWidget(openButton, 1);
	fileLayout->addWidget(fileLabel, 8);
	
	// Convert
	QVBoxLayout *convertLayout = new QVBoxLayout();

	tmpLayout = new QHBoxLayout();
	outputEdit = new QLineEdit();
	outputEdit->setFont(QFont("Courier New", 10));
	tmpLayout->addWidget(new QLabel("Output: "));
	tmpLayout->addWidget(outputEdit);
	convertLayout->addLayout(tmpLayout);

	tmpLayout = new QHBoxLayout();
	convertButton = new QPushButton("Convert!");
	convertButton->setSizePolicy(QSizePolicy::Expanding,
		QSizePolicy::Expanding);
	connect(convertButton, SIGNAL(clicked()), this, SLOT(convertButtonClicked()));
	tmpLayout->addWidget(lpa, 3);
	tmpLayout->addWidget(convertButton, 1);
	convertLayout->addLayout(tmpLayout);

	convertGroup = new QGroupBox("Convert");
	convertGroup->setLayout(convertLayout);

	// All
	QVBoxLayout *layout = new QVBoxLayout();
	layout->addLayout(fileLayout, 0);
	layout->addWidget(convertGroup, 1);
	
	setLayout(layout);
}


XLpaTab::XLpaTab(QWidget *parent) : QWidget(parent) {
	fileName = "../data/1874.lpa";
	lpa = new XLpa();
	setupUI();

	fileLabel->setText(fileName);
	outputEdit->setText(deleteExt(fileName) + "-lpa.wav");
	lpa->update(fileName);
}
